﻿using System.Linq;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.jass.DAL;

namespace iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL
{
    public class AdvertisementDAO : ObjectDAO, IAdvertisement
    {
        public IQueryable<Advertisement> FindAllAdvertisements()
        {
            return jassEntities.Advertisements;
        }

        public Advertisement GetAdvertisement(int advertisementId)
        {
            return jassEntities.Advertisements.SingleOrDefault<Advertisement>(m => m.Id == advertisementId);
        }

        public void AddAdvertisement(Advertisement advertisement)
        {
            jassEntities.AddToAdvertisements(advertisement);
            Save();
        }

        public void UpdateAdvertisement(Advertisement advertisement)
        {
            jassEntities.Advertisements.ApplyCurrentValues(advertisement);
            Save();
        }

        public void DeleteAdvertisement(int advertisementId)
        {
            Advertisement advertisement = GetAdvertisement(advertisementId);
            jassEntities.Advertisements.DeleteObject(advertisement);
            Save();
        }


        public Company GetCompanyByAdvertisementId(int advertisementId)
        {
            return GetAdvertisement(advertisementId).Company;
        }

        public GeoArea GetGeographicalAreaByAdvertisementId(int advertisementId)
        {
            return GetAdvertisement(advertisementId).GeoArea;
        }

        public AdvertisementType GetAdvertisementTypeByAdvertisementId(int advertisementId)
        {
            return GetAdvertisement(advertisementId).AdvertisementType;
        }


        public IQueryable<Advertisement> FindAllAdvertisementsByUser(System.Guid userId)
        {
            return jassEntities.Advertisements.Where(m => m.UserId == userId);
        }
    }

    public class AdvertisementSearchLogDAO : ObjectDAO, IAdvertisementSearchLog
    {
        public IQueryable<AdvertisementSearchLog> FindAllAdvertisementSearchLogs()
        {
            return jassEntities.AdvertisementSearchLogs;
        }

        public AdvertisementSearchLog GetAdvertisementSearchLog(int advertisementSearchLogId)
        {
            return jassEntities.AdvertisementSearchLogs.SingleOrDefault(m => m.Id == advertisementSearchLogId);
        }

        public void AddAdvertisementSearchLog(AdvertisementSearchLog advertisementSearchLog)
        {
            jassEntities.AddToAdvertisementSearchLogs(advertisementSearchLog);
            Save();
        }

        public void UpdateAdvertisementSearchLog(AdvertisementSearchLog advertisementSearchLog)
        {
            jassEntities.AdvertisementSearchLogs.ApplyCurrentValues(advertisementSearchLog);
            Save();
        }

        public void DeleteAdvertisementSearchLog(int advertisementSearchLogId)
        {
            AdvertisementSearchLog advertisementSearchLog = GetAdvertisementSearchLog(advertisementSearchLogId);
            jassEntities.AdvertisementSearchLogs.DeleteObject(advertisementSearchLog);
            Save();
        }
    }

    public class AdvertisementTypeDAO : ObjectDAO, IAdvertisementType
    {
        public IQueryable<AdvertisementType> FindAllAdvertisementTypes()
        {
            return jassEntities.AdvertisementTypes;
        }

        public AdvertisementType GetAdvertisementType(int advertisementTypeId)
        {
            return jassEntities.AdvertisementTypes.SingleOrDefault(m => m.Id == advertisementTypeId);
        }

        public void AddAdvertisementType(AdvertisementType advertisementType)
        {
            jassEntities.AddToAdvertisementTypes(advertisementType);
            Save();
        }

        public void UpdateAdvertisementType(AdvertisementType advertisementType)
        {
            jassEntities.AdvertisementTypes.ApplyCurrentValues(advertisementType);
            Save();
        }

        public void DeleteAdvertisementType(int advertisementTypeId)
        {
            AdvertisementType advertisementType = GetAdvertisementType(advertisementTypeId);
            jassEntities.AdvertisementTypes.DeleteObject(advertisementType);
            Save();
        }
    }

    public class AdvertisementViewLogDAO : ObjectDAO, IAdvertisementViewLog
    {
        public IQueryable<AdvertisementViewLog> FindAllAdvertisementViewLogs()
        {
            return jassEntities.AdvertisementViewLogs;
        }

        public AdvertisementViewLog GetAdvertisementViewLog(int advertisementViewLogId)
        {
            return jassEntities.AdvertisementViewLogs.SingleOrDefault(m => m.Id == advertisementViewLogId);
        }

        public void AddAdvertisementViewLog(AdvertisementViewLog advertisementViewLog)
        {
            jassEntities.AddToAdvertisementViewLogs(advertisementViewLog);
            Save();
        }

        public void UpdateAdvertisementViewLog(AdvertisementViewLog advertisementViewLog)
        {
            jassEntities.AdvertisementViewLogs.ApplyCurrentValues(advertisementViewLog);
            Save();
        }

        public void DeleteAdvertisementViewLog(int advertisementViewLogId)
        {
            jassEntities.AdvertisementViewLogs.DeleteObject(GetAdvertisementViewLog(advertisementViewLogId));
            Save();
        }
    }
}
